package com.EAIPClub.userauth.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.EAIPClub.userauth.service.LoginService;
import com.api.API.UserAuthDataManager.UserAuthDataManagerAPI;
import com.api.API.UserDataManager.UserDataManagerAPI;
import com.api.ConstantPool;
import com.api.POJO.UserAuth.Account;
import com.api.POJO.UserAuth.AuthReturnMsg;
import com.api.POJO.UserAuth.RegisterAccount;
import com.api.POJO.UserAuth.RegisterReturnMsg;
import com.api.POJO.UserAuthDataManager.UserAuthData;
import com.api.POJO.UserDataManager.Result.UserDataReturnMsg;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @Description :用户登录业务逻辑层实现
 * @Author :崔昌赫
 * @Date : 2021/10/15
 */
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    private UserDataManagerAPI userDataManagerAPI;

    @Autowired
    public void setUserDataManagerAPI(UserDataManagerAPI userDataManagerAPI) {
        this.userDataManagerAPI = userDataManagerAPI;
    }

    private UserAuthDataManagerAPI authDataManagerAPI;

    @Autowired
    public void setAuthDataManagerAPI(UserAuthDataManagerAPI authDataManagerAPI) {
        this.authDataManagerAPI = authDataManagerAPI;
    }

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @SneakyThrows
    @Override
    public AuthReturnMsg login(Account account) {
        log.info("即将开始登录验证:{}",account);
        UserDataReturnMsg userData = getUserData(account.getEmail());
        AuthReturnMsg returnMsg = new AuthReturnMsg();
        List<String> Msg= new ArrayList<>();
        log.info("获取到的信息为:{}",userData);
        if (!userData.getStatus().equals(ConstantPool.OK)) {
            log.warn("用户登录失败！数据为:{},原因:{}",account,userData.getMsg());
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("登陆失败！请检查您的用户名或密码！");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        String email = userData.getUser().getEmail();
        String password =userData.getUser().getPassword();
        if(!(account.getEmail().equals(email)&&account.getPassword().equals(password))){
            log.warn("用户登录失败！数据为:{},原因:用户名或密码错误!",account);
            returnMsg.setStatus(ConstantPool.BadRequest);
            Msg.add("用户名或密码错误!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        Date date = new Date();
        Date endDate=new Date(date.getTime()+21600000);
        String token =SecureUtil.md5(account.getEmail()+simpleDateFormat.format(date)+simpleDateFormat.format(endDate));
        log.info("用户:{},登陆时间:{},到期时间:{},生成token:{}",account.getEmail(),simpleDateFormat.format(date),simpleDateFormat.format(endDate),token);
        addUserAuthData(userData.getUser().getUid(),token,simpleDateFormat.format(date),simpleDateFormat.format(endDate));
        returnMsg.setToken(token);
        returnMsg.setEmail(email);
        returnMsg.setStatus(200);
        Msg.add("登陆成功!");
        return returnMsg;
    }


    public UserDataReturnMsg getUserData(String email){
        log.info("开始查询用户基本数据,email:{}",email);
        UserDataReturnMsg userAllInfoByEmail = userDataManagerAPI.findUserAllInfoByEmail(email);
        log.info("查询到的基本数据为:{}",userAllInfoByEmail);
        return userAllInfoByEmail;
    }

    @Async
    @Transactional
    public void addUserAuthData(String uid,String token,String date,String expiration){
        log.info("即将将登录信息加入到数据库.uid:{},token:{},date:{},expiration:{}",uid,token,date,expiration);
        authDataManagerAPI.addNewUserAuthData(new UserAuthData(uid,token,date,expiration));
    }

}
